package com.financing.wap.service.impl;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.financing.wap.dao.IProductDao;
import com.financing.wap.dao.IProductDeepAnalyticsDao;
import com.financing.wap.dto.FpProductDeepAnalyticsDTO;
import com.financing.wap.dto.FpProjectAnnualRevenue;
import com.financing.wap.dto.FpProjectRabate;
import com.financing.wap.dto.ProductBasicInfoDTO;
import com.financing.wap.service.IProductDeepAnalyticsService;


@Service("productDeepAnalyticsService")
public class ProductDeepAnalyticsServiceImpl implements IProductDeepAnalyticsService{

	Logger logger = Logger.getLogger(getClass());

	@Autowired
	private IProductDeepAnalyticsDao productDeepAnalyticsDao;
	@Autowired
	private IProductDao productDao;

	@Override
	public FpProductDeepAnalyticsDTO getFpProductDeepAnalyticsView(Map map) {
		FpProductDeepAnalyticsDTO bean = productDeepAnalyticsDao.getFpProductDeepAnalyticsView(map);
		if(bean == null){
			return null;
		}
		StringBuilder annualRevenueExpect = new StringBuilder();
		List<FpProjectRabate> rebateList = productDao.getProductProfitRebateList(bean.getFpProductId());
		for(FpProjectRabate fpProjectRabate:rebateList){
			FpProjectAnnualRevenue revenueBean = fpProjectRabate.getAnnualRevenue();
			annualRevenueExpect.append(checkOrderAmount(revenueBean.getPStartAmount()));
			annualRevenueExpect.append("万元-");
			annualRevenueExpect.append(checkOrderAmount(revenueBean.getPEndAmount()));
			annualRevenueExpect.append("万元,预期收益率为【");
			annualRevenueExpect.append(revenueBean.getPAnnualRevenueExpect());
			annualRevenueExpect.append("】;");
		}
		ProductBasicInfoDTO productBasicInfoDTO= productDao.queryBasicInfo(map.get("productCode").toString());
		if(productBasicInfoDTO != null){
			productBasicInfoDTO.setAnnualRevenueExpect(annualRevenueExpect.toString());
		}
		bean.setProductDTO(productBasicInfoDTO);
		return bean;
	}
	private String checkOrderAmount(BigDecimal orderAmount){
		if(orderAmount== null){
			return "";
		}
		return orderAmount.divide(new BigDecimal(10000),2, BigDecimal.ROUND_HALF_UP).toString();
	}
	
}
